package cn.exrick.xboot.flow.service.serviceimpl;

import cn.exrick.xboot.flow.entity.FlowNodeHandler;
import cn.exrick.xboot.flow.entity.FlowNodeinfo;
import cn.exrick.xboot.flow.entity.FlowTaskHandler;
import cn.exrick.xboot.flow.entity.FlowTaskInstance;
import cn.exrick.xboot.flow.mapper.FlowTaskHandlerMapper;
import cn.exrick.xboot.flow.service.IFlowNodeHandlerService;
import cn.exrick.xboot.flow.service.IFlowNodeinfoService;
import cn.exrick.xboot.flow.service.IFlowTaskHandlerService;
import cn.exrick.xboot.modules.content.dao.mapper.HontUserMapper;
import cn.exrick.xboot.modules.content.entity.HontVo.HontUserRolelevelQueryVo;
import cn.exrick.xboot.modules.content.entity.HontVo.HontUserRolelevelVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-09-07
 */
@Service
public class FlowTaskHandlerServiceImpl extends ServiceImpl<FlowTaskHandlerMapper, FlowTaskHandler> implements IFlowTaskHandlerService {
    @Autowired
    private FlowTaskHandlerMapper flowTaskHandlerMapper;
    @Autowired
    private IFlowNodeHandlerService iFlowNodeHandlerService;
    @Autowired
    private IFlowNodeinfoService iFlowNodeinfoService;
    @Autowired
    private HontUserMapper hontUserMapper;
    @Override
    public FlowNodeinfo addFlowTaskHandler(FlowNodeinfo flowNodeinfo,Integer level,
                                   String hontDepartmentId,String taskId) {
        List<FlowTaskHandler> flowTaskHandlers=new ArrayList<>();
        List<FlowNodeHandler> flowNodeHandlers=
                iFlowNodeHandlerService.getFlowNodeHandlerOnUser(flowNodeinfo.getId(),level);
        if(flowNodeHandlers.size()==0){
            if(FlowNodeinfo.NODE_TYPE_FINISH.equals(flowNodeinfo.getNodeType())){
                return null;
            }
            FlowNodeinfo flowNodeinfo1=
                    iFlowNodeinfoService.getFlowNodeinfo(flowNodeinfo.getTemplateId(),flowNodeinfo.getTemplateVersion(),flowNodeinfo.getSetpId()+1);
            return addFlowTaskHandler(flowNodeinfo1,level,hontDepartmentId,taskId);
        }
        if(flowNodeinfo.getApprovalType()==FlowNodeinfo.NODEHANDLER_TYPE_USER){
            //添加用户审批人
            List<FlowNodeHandler> userNodeHandlers=
                    flowNodeHandlers.stream().filter(t->hontDepartmentId.equals(t.getHontDepartmentId())).collect(Collectors.toList());
            if(userNodeHandlers.size()==0){
                return null;
            }
            for (FlowNodeHandler item:userNodeHandlers){
                FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
                flowTaskHandler.setTaskId(taskId);
                flowTaskHandler.setHontUserId(item.getNodeHandlerId());
                flowTaskHandlers.add(flowTaskHandler);
            }
        }else {
            //添加角色审批人
            List<String> rloeids=flowNodeHandlers.stream().map(t->t.getNodeHandlerId()).collect(Collectors.toList());
            HontUserRolelevelQueryVo hontUserRolelevelQueryVo=new HontUserRolelevelQueryVo();
            hontUserRolelevelQueryVo.setRoleIds(rloeids);
            List<HontUserRolelevelVo> userRolelevelVos=
                    hontUserMapper.getHontUserAndRoleAndLevel(hontUserRolelevelQueryVo);
            for (HontUserRolelevelVo item:userRolelevelVos){
                FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
                flowTaskHandler.setTaskId(taskId);
                flowTaskHandler.setHontUserId(item.getId());
                flowTaskHandler.setHontUserName(item.getNickName());
                flowTaskHandlers.add(flowTaskHandler);
            }
        }
        flowTaskHandlerMapper.insertBatch(flowTaskHandlers);
        return flowNodeinfo;

    }

    @Override
    public boolean checkUser(String taskId, String userId) {
        QueryWrapper<FlowTaskHandler> query = Wrappers.query();
        query.lambda().eq(FlowTaskHandler::getTaskId,taskId)
                .eq(FlowTaskHandler::getHontUserId,userId);
        int count=flowTaskHandlerMapper.selectCount(query);
        if(count>0){
            return true;
        }
        return false;
    }

    @Override
    public void deleteByTaskId(String taskId) {
        QueryWrapper<FlowTaskHandler> query = Wrappers.query();
        query.lambda().eq(FlowTaskHandler::getTaskId,taskId);
        flowTaskHandlerMapper.delete(query);
    }

    @Override
    public void updateFlowTaskHandler(FlowTaskInstance flowTaskInstance,String hontDepartmentId) {
        deleteByTaskId(flowTaskInstance.getId());

        List<FlowTaskHandler> flowTaskHandlers=new ArrayList<>();
        FlowNodeinfo flowNodeinfo=iFlowNodeinfoService.getById(flowTaskInstance.getFlowNodeId());
        List<FlowNodeHandler> flowNodeHandlers=
                iFlowNodeHandlerService.getFlowNodeHandlerOnUser(flowTaskInstance.getFlowNodeId(),null);
       if(flowNodeinfo.getApprovalType()==FlowNodeinfo.NODEHANDLER_TYPE_USER){

           List<FlowNodeHandler> userNodeHandlers=
                   flowNodeHandlers.stream().filter(t->hontDepartmentId.equals(t.getHontDepartmentId())).collect(Collectors.toList());
           if(userNodeHandlers.size()==0){
               return ;
           }
           for (FlowNodeHandler item:userNodeHandlers){
               FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
               flowTaskHandler.setTaskId(flowTaskInstance.getId());
               flowTaskHandler.setHontUserId(item.getNodeHandlerId());
               flowTaskHandlers.add(flowTaskHandler);
           }
       }else {
           List<String> rloeids=flowNodeHandlers.stream().map(t->t.getNodeHandlerId()).collect(Collectors.toList());
           HontUserRolelevelQueryVo hontUserRolelevelQueryVo=new HontUserRolelevelQueryVo();
           hontUserRolelevelQueryVo.setRoleIds(rloeids);
           List<HontUserRolelevelVo> userRolelevelVos=
                   hontUserMapper.getHontUserAndRoleAndLevel(hontUserRolelevelQueryVo);
           for (HontUserRolelevelVo item:userRolelevelVos){
               FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
               flowTaskHandler.setTaskId(flowTaskInstance.getId());
               flowTaskHandler.setHontUserId(item.getId());
               flowTaskHandler.setHontUserName(item.getNickName());
               flowTaskHandlers.add(flowTaskHandler);
           }
       }

        flowTaskHandlerMapper.insertBatch(flowTaskHandlers);
    }
}
